package com.tgh.gles2.c7.pointSprites;

import com.tgh.gles2.BaseRenderer;
import com.tgh.gles2.R;
import com.tgh.utils.GLBatch;
import com.tgh.utils.GLFrustum;
import com.tgh.utils.GLShaderManager;
import com.tgh.utils.GLTools;
import com.tgh.utils.Vector4f;

import android.content.Context;
import android.opengl.GLES20;
import android.os.SystemClock;

import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class SpaceFlightRenderer extends BaseRenderer {

    public SpaceFlightRenderer(Context context) {
        super(context);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        initProgram();
        initTexture();
        GLES20.glClearColor(0, 0, 0, 1);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        initVertices(width,height);
        GLES20.glViewport(0, 0, width, height);
        viewFrustum=new GLFrustum(35, (float)width/(float)height, 1, 1000);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        
        GLES20.glEnable(GLES20.GL_BLEND);
        GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE);
        
        GLES20.glUseProgram(spaceShader);
        
        GLES20.glUniformMatrix4fv(locMvpMatrix, 1, false, viewFrustum.getProjectionMatrix().getArray(), 0);
        
        float fTime=(SystemClock.elapsedRealtime()/100)%999.0f;
        GLES20.glUniform1f(locTimeStamp, fTime);
        
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, starTexture);
        GLES20.glUniform1i(locStarImage, 0);
        starBatch.draw();
        GLES20.glDisable(GLES20.GL_BLEND);
    }

    @Override
    public void onSurfaceDestroyed() {
    }

    private void initProgram() {
        String vpSrc=GLShaderManager.readFileFromAssets(mContext, "SpaceFlight.vp");
        String fpSrc=GLShaderManager.readFileFromAssets(mContext, "SpaceFlight.fp");
        spaceShader = GLShaderManager.createProgram(vpSrc, fpSrc, 
                new int[]{GLShaderManager.GL_ATTRIBUTE_VERTEX,GLShaderManager.GL_ATTRIBUTE_COLOR}, 
                new String[]{"vVertex","vColor"});
        locMvpMatrix = GLES20.glGetUniformLocation(spaceShader, "mvpMatrix");
        locTimeStamp = GLES20.glGetUniformLocation(spaceShader, "timeStamp");
        locStarImage = GLES20.glGetUniformLocation(spaceShader, "starImage");
    }
    
    private void initTexture(){
        int[] textures = GLTools.genTextures(1);
        starTexture=textures[0];
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, starTexture);
        GLTools.loadDrawableToTexture2D(
                mContext, 
                R.drawable.star, 
                GLES20.GL_LINEAR_MIPMAP_LINEAR,
                GLES20.GL_LINEAR, 
                GLES20.GL_CLAMP_TO_EDGE, 
                true);
    }
    
    private void initVertices(int w,int h) {
        Vector4f[] colors={
                new Vector4f(1.0f, 1.0f, 1.0f, 1.0f),
                new Vector4f(0.67f, 0.68f, 0.82f, 1.0f),
                new Vector4f(1.0f, 0.5f, 0.5f, 1.0f),
                new Vector4f(1.0f, 0.82f, 0.65f, 1.0f)
        };
        starBatch = new GLBatch(GLES20.GL_POINTS, STAR_NUMS);
        Random random=new Random();
        for (int i = 0; i < STAR_NUMS; i++) {
            int iColor=random.nextInt(4);
            starBatch.color(colors[iColor]);
            float x=random.nextInt(w)-w/2;
            float y=random.nextInt(h)-h/2;
            float z=-(random.nextInt(1000)-1);
            starBatch.vertex(x, y, z);
        }
        starBatch.end();
    }
    
    private static final int STAR_NUMS=10000;
    private GLBatch starBatch;
    private int spaceShader;
    private int locMvpMatrix;
    private int locTimeStamp;
    private int locStarImage;
    private int starTexture;
    private GLFrustum viewFrustum;
}
